/*
 * Copyright 2002-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gitee.l0km.aocache;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;

import com.google.common.primitives.Primitives;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.gitee.l0km.aocache.AocacheUtils.hasText; 
import static com.gitee.l0km.aocache.AocacheUtils.arrayToDelimitedString; 


/**
 * {@link LinkedHashMap} subclass representing annotation attribute
 * <em>key-value</em> pairs
 *
 * <p>Provides 'pseudo-reification' to avoid noisy Map generics in the calling
 * code as well as convenience methods for looking up annotation attributes
 * in a type-safe fashion.
 *
 * @author Chris Beams
 * @author Sam Brannen
 * @author Juergen Hoeller
 */
@SuppressWarnings("serial")
public class AnnotationAttributes extends LinkedHashMap<String, Object>{

	private static final String UNKNOWN = "unknown";
	private final Class<? extends Annotation> annotationType;

	final String displayName;

	boolean validated = false;
	
	/**
	 * 实例是否可以修改
	 */
	protected boolean mutable = true;

	/**
	 * Create a new, empty {@link AnnotationAttributes} instance.
	 */
	public AnnotationAttributes() {
		this.annotationType = null;
		this.displayName = UNKNOWN;
	}

	/**
	 * Create a new, empty {@link AnnotationAttributes} instance with the
	 * given initial capacity to optimize performance.
	 * @param initialCapacity initial size of the underlying map
	 */
	public AnnotationAttributes(int initialCapacity) {
		super(initialCapacity);
		this.annotationType = null;
		this.displayName = UNKNOWN;
		this.mutable = true;
	}

	/**
	 * Create a new {@link AnnotationAttributes} instance, wrapping the provided
	 * map and all its <em>key-value</em> pairs.
	 * @param map original source of annotation attribute <em>key-value</em> pairs
	 */
	public AnnotationAttributes(Map<String, Object> map) {
		super(map);
		this.annotationType = null;
		this.displayName = UNKNOWN;
	}

	/**
	 * Create a new {@link AnnotationAttributes} instance, wrapping the provided
	 * map and all its <em>key-value</em> pairs.
	 * @param other original source of annotation attribute <em>key-value</em> pairs
	 */
	public AnnotationAttributes(AnnotationAttributes other) {
		this(other,other.mutable);
	}
	/**
	 * Create a new {@link AnnotationAttributes} instance, wrapping the provided
	 * map and all its <em>key-value</em> pairs.
	 * @param other original source of annotation attribute <em>key-value</em> pairs
	 * @param annotationType 注解类型，为{@code null}使用 other的annotationType字段代替
	 * @param mutable 新实例是否可修改
	 */
	public AnnotationAttributes(AnnotationAttributes other,Class<? extends Annotation> annotationType,boolean mutable) {
		super(other);
		this.annotationType = null == annotationType ? other.annotationType:annotationType ;
		this.displayName = other.displayName;
		this.validated = other.validated;
		this.mutable = mutable;
	}
	/**
	 * Create a new {@link AnnotationAttributes} instance, wrapping the provided
	 * map and all its <em>key-value</em> pairs.
	 * @param other original source of annotation attribute <em>key-value</em> pairs
	 * @param mutable 新实例是否可修改
	 */
	public AnnotationAttributes(AnnotationAttributes other,boolean mutable) {
		this(other,null,mutable);
	}

	/**
	 * Create a new, empty {@link AnnotationAttributes} instance for the
	 * specified {@code annotationType}.
	 * @param annotation the type of annotation represented by this
	 * {@code AnnotationAttributes} instance; never {@code null}
	 */
	public AnnotationAttributes(Annotation annotation) {
		this.annotationType = checkNotNull(annotation, "'annotation' must not be null").annotationType();
		this.displayName = this.annotationType.getName();
		addAll(annotation);
	}
	/**
	 * Create a new, empty {@link AnnotationAttributes} instance for the
	 * specified {@code annotationType}.
	 * @param annotationType the type of annotation represented by this
	 * {@code AnnotationAttributes} instance; never {@code null}
	 */
	public AnnotationAttributes(Class<? extends Annotation> annotationType) {
		checkNotNull(annotationType, "'annotationType' must not be null");
		this.annotationType = annotationType;
		this.displayName = annotationType.getName();
	}

	/**
	 * Create a possibly already validated new, empty
	 * {@link AnnotationAttributes} instance for the specified
	 * {@code annotationType}.
	 * @param annotationType the type of annotation represented by this
	 * {@code AnnotationAttributes} instance; never {@code null}
	 * @param validated if the attributes are considered already validated
	 */
	AnnotationAttributes(Class<? extends Annotation> annotationType, boolean validated) {
		checkNotNull(annotationType, "'annotationType' must not be null");
		this.annotationType = annotationType;
		this.displayName = annotationType.getName();
		this.validated = validated;
	}

	/**
	 * Create a new, empty {@link AnnotationAttributes} instance for the
	 * specified {@code annotationType}.
	 * @param annotationType the annotation type name represented by this
	 * {@code AnnotationAttributes} instance; never {@code null}
	 * @param classLoader the ClassLoader to try to load the annotation type on,
	 * or {@code null} to just store the annotation type name
	 */
	public AnnotationAttributes(String annotationType, ClassLoader classLoader) {
		checkNotNull(annotationType, "'annotationType' must not be null");
		this.annotationType = getAnnotationType(annotationType, classLoader);
		this.displayName = annotationType;
	}

	@SuppressWarnings("unchecked")
	private static Class<? extends Annotation> getAnnotationType(String annotationType, ClassLoader classLoader) {
		if (classLoader != null) {
			try {
				return (Class<? extends Annotation>) classLoader.loadClass(annotationType);
			}
			catch (ClassNotFoundException ex) {
				// Annotation Class not resolvable
			}
		}
		return null;
	}


	/**
	 * Get the type of annotation represented by this {@code AnnotationAttributes}.
	 * @return the annotation type, or {@code null} if unknown
	 */
	public Class<? extends Annotation> annotationType() {
		return this.annotationType;
	}

	/**
	 * Get the value stored under the specified {@code attributeName} as a string.
	 * @param attributeName the name of the attribute to get;
	 * never {@code null} or empty
	 * @return the value
	 * @throws IllegalArgumentException if the attribute does not exist or
	 * if it is not of the expected type
	 */
	public String getString(String attributeName) {
		return getRequiredAttribute(attributeName, String.class);
	}

	/**
	 * Get the value stored under the specified {@code attributeName} as an
	 * array of strings.
	 * <p>If the value stored under the specified {@code attributeName} is
	 * a string, it will be wrapped in a single-element array before
	 * returning it.
	 * @param attributeName the name of the attribute to get;
	 * never {@code null} or empty
	 * @return the value
	 * @throws IllegalArgumentException if the attribute does not exist or
	 * if it is not of the expected type
	 */
	public String[] getStringArray(String attributeName) {
		return getRequiredAttribute(attributeName, String[].class);
	}

	/**
	 * Get the value stored under the specified {@code attributeName} as a boolean.
	 * @param attributeName the name of the attribute to get;
	 * never {@code null} or empty
	 * @return the value
	 * @throws IllegalArgumentException if the attribute does not exist or
	 * if it is not of the expected type
	 */
	public boolean getBoolean(String attributeName) {
		return getRequiredAttribute(attributeName, Boolean.class);
	}

	/**
	 * Get the value stored under the specified {@code attributeName} as a number.
	 * @param attributeName the name of the attribute to get;
	 * never {@code null} or empty
	 * @return the value
	 * @throws IllegalArgumentException if the attribute does not exist or
	 * if it is not of the expected type
	 */
	@SuppressWarnings("unchecked")
	public <N extends Number> N getNumber(String attributeName) {
		return (N) getRequiredAttribute(attributeName, Number.class);
	}

	/**
	 * Get the value stored under the specified {@code attributeName} as an enum.
	 * @param attributeName the name of the attribute to get;
	 * never {@code null} or empty
	 * @return the value
	 * @throws IllegalArgumentException if the attribute does not exist or
	 * if it is not of the expected type
	 */
	@SuppressWarnings("unchecked")
	public <E extends Enum<?>> E getEnum(String attributeName) {
		return (E) getRequiredAttribute(attributeName, Enum.class);
	}

	/**
	 * Get the value stored under the specified {@code attributeName} as a class.
	 * @param attributeName the name of the attribute to get;
	 * never {@code null} or empty
	 * @return the value
	 * @throws IllegalArgumentException if the attribute does not exist or
	 * if it is not of the expected type
	 */
	@SuppressWarnings("unchecked")
	public <T> Class<? extends T> getClass(String attributeName) {
		return getRequiredAttribute(attributeName, Class.class);
	}

	/**
	 * Get the value stored under the specified {@code attributeName} as an
	 * array of classes.
	 * <p>If the value stored under the specified {@code attributeName} is a class,
	 * it will be wrapped in a single-element array before returning it.
	 * @param attributeName the name of the attribute to get;
	 * never {@code null} or empty
	 * @return the value
	 * @throws IllegalArgumentException if the attribute does not exist or
	 * if it is not of the expected type
	 */
	public Class<?>[] getClassArray(String attributeName) {
		return getRequiredAttribute(attributeName, Class[].class);
	}

	/**
	 * Get the {@link AnnotationAttributes} stored under the specified
	 * {@code attributeName}.
	 * <p>Note: if you expect an actual annotation, invoke
	 * {@link #getAnnotation(String, Class)} instead.
	 * @param attributeName the name of the attribute to get;
	 * never {@code null} or empty
	 * @return the {@code AnnotationAttributes}
	 * @throws IllegalArgumentException if the attribute does not exist or
	 * if it is not of the expected type
	 */
	public AnnotationAttributes getAnnotation(String attributeName) {
		return getRequiredAttribute(attributeName, AnnotationAttributes.class);
	}

	/**
	 * Get the annotation of type {@code annotationType} stored under the
	 * specified {@code attributeName}.
	 * @param attributeName the name of the attribute to get;
	 * never {@code null} or empty
	 * @param annotationType the expected annotation type; never {@code null}
	 * @return the annotation
	 * @throws IllegalArgumentException if the attribute does not exist or
	 * if it is not of the expected type
	 */
	public <A extends Annotation> A getAnnotation(String attributeName, Class<A> annotationType) {
		return getRequiredAttribute(attributeName, annotationType);
	}

	/**
	 * Get the array of {@link AnnotationAttributes} stored under the specified
	 * {@code attributeName}.
	 * <p>If the value stored under the specified {@code attributeName} is
	 * an instance of {@code AnnotationAttributes}, it will be wrapped in
	 * a single-element array before returning it.
	 * <p>Note: if you expect an actual array of annotations, invoke
	 * {@link #getAnnotationArray(String, Class)} instead.
	 * @param attributeName the name of the attribute to get;
	 * never {@code null} or empty
	 * @return the array of {@code AnnotationAttributes}
	 * @throws IllegalArgumentException if the attribute does not exist or
	 * if it is not of the expected type
	 */
	public AnnotationAttributes[] getAnnotationArray(String attributeName) {
		return getRequiredAttribute(attributeName, AnnotationAttributes[].class);
	}

	/**
	 * Get the array of type {@code annotationType} stored under the specified
	 * {@code attributeName}.
	 * <p>If the value stored under the specified {@code attributeName} is
	 * an {@code Annotation}, it will be wrapped in a single-element array
	 * before returning it.
	 * @param attributeName the name of the attribute to get;
	 * never {@code null} or empty
	 * @param annotationType the expected annotation type; never {@code null}
	 * @return the annotation array
	 * @throws IllegalArgumentException if the attribute does not exist or
	 * if it is not of the expected type
	 */
	@SuppressWarnings("unchecked")
	public <A extends Annotation> A[] getAnnotationArray(String attributeName, Class<A> annotationType) {
		Object array = Array.newInstance(annotationType, 0);
		return (A[]) getRequiredAttribute(attributeName, array.getClass());
	}

	/**
	 * Get the value stored under the specified {@code attributeName},
	 * ensuring that the value is of the {@code expectedType}.
	 * <p>If the {@code expectedType} is an array and the value stored
	 * under the specified {@code attributeName} is a single element of the
	 * component type of the expected array type, the single element will be
	 * wrapped in a single-element array of the appropriate type before
	 * returning it.
	 * @param attributeName the name of the attribute to get;
	 * never {@code null} or empty
	 * @param expectedType the expected type; never {@code null}
	 * @return the value
	 * @throws IllegalArgumentException if the attribute does not exist or
	 * if it is not of the expected type
	 */
	@SuppressWarnings("unchecked")
	private <T> T getRequiredAttribute(String attributeName, Class<T> expectedType) {
		hasText(attributeName, "'attributeName' must not be null or empty");
		Object value = get(attributeName);
		assertAttributePresence(attributeName, value);
		assertNotException(attributeName, value);
		if (!expectedType.isInstance(value) && expectedType.isArray() &&
				expectedType.getComponentType().isInstance(value)) {
			Object array = Array.newInstance(expectedType.getComponentType(), 1);
			Array.set(array, 0, value);
			value = array;
		}
		assertAttributeType(attributeName, value, expectedType);
		return (T) value;
	}

	private void assertAttributePresence(String attributeName, Object attributeValue) {
		checkNotNull(attributeValue,String.format(
				"Attribute '%s' not found in attributes for annotation [%s]",
				attributeName, this.displayName));
	}

	private void assertNotException(String attributeName, Object attributeValue) {
		if (attributeValue instanceof Throwable) {
			throw new IllegalArgumentException(String.format(
					"Attribute '%s' for annotation [%s] was not resolvable due to exception [%s]",
					attributeName, this.displayName, attributeValue), (Throwable) attributeValue);
		}
	}

	private void assertAttributeType(String attributeName, Object attributeValue, Class<?> expectedType) {
		if (!expectedType.isInstance(attributeValue)) {
			throw new IllegalArgumentException(String.format(
					"Attribute '%s' is of type %s, but %s was expected in attributes for annotation [%s]",
					attributeName, attributeValue.getClass().getSimpleName(), expectedType.getSimpleName(),
					this.displayName));
		}
	}

	@Override
	public String toString() {
		Iterator<Map.Entry<String, Object>> entries = entrySet().iterator();
		StringBuilder sb = new StringBuilder("{");
		while (entries.hasNext()) {
			Map.Entry<String, Object> entry = entries.next();
			sb.append(entry.getKey());
			sb.append('=');
			sb.append(valueToString(entry.getValue()));
			if (entries.hasNext()) {
				sb.append(", ");
			}
		}
		sb.append('}');
		return sb.toString();
	}
	private String valueToString(Object value) {
		if (value == this) {
			return "(this Map)";
		}
		if (value instanceof Object[]) {
			return "[" + arrayToDelimitedString((Object[]) value, ", ") + "]";
		}
		return String.valueOf(value);
	}

	/**
	 * Create a type-safe synthesized version of this merged annotation that can
	 * be used directly in code.
	 * <p>The result is synthesized using a JDK {@link Proxy} and as a result may
	 * incur a computational cost when first invoked.
	 * @return a synthesized version of the annotation or the original annotation
	 * unmodified
	 */
    @SuppressWarnings("unchecked")
	public<A extends Annotation>  A synthesize() {
        return (A) synthesize(annotationType);
    }
    /**
     * Create a type-safe synthesized version of this merged annotation that can
     * be used directly in code.
     * <p>The result is synthesized using a JDK {@link Proxy} and as a result may
     * incur a computational cost when first invoked.
     * @return a synthesized version of the annotation or the original annotation
     * unmodified
     */
    public<A extends Annotation>  A synthesize(Class<A> annotationType) {
    	return SynthesizedAnnotationInvocationHandler.createProxy(this, annotationType);
    }
	/**
	 * 获取方法名指定值如果为空则返回方法默认值({@link Method#getDefaultValue()})，
	 * 如果没有默认值则抛出异常
	 * @param annotMethod
	 * @throws NoSuchElementException
	 */
	private Object valueOrDefault(Method annotMethod) throws NoSuchElementException {
		Object value = get(annotMethod.getName());
		if(null == value) {
			value = annotMethod.getDefaultValue();
		}
		if(null==value) {
			throw new NoSuchElementException("No value found for attribute named '" + annotMethod.getName() +
					"' in annotation " + annotMethod.getDeclaringClass().getName());
		}
		return value;
	}
	Object readValidValueOf(Method annotMethod) {
		return valiidateType(annotMethod, valueOrDefault(annotMethod));
	}

	/**
	 * 验证值是否是匹配方法的返回值类型不匹配则抛出异常
	 * @param annotMethod
	 * @param value
	 * @return always value
	 */
	private static Object valiidateType(Method annotMethod,Object value) {
		checkArgument(Primitives.wrap(annotMethod.getReturnType()).isInstance(value),
				"value: %s MISMATCH TYPE FOR %s",String.valueOf(value),annotMethod.getName());
		return value;
	}
	/**
	 * 验证键值对是否是当前 {@link #annotationType} 的字段类型不匹配则抛出异常
	 * @param key
	 * @param value
	 */
	private void valiidateType(String key,Object value) {
		if(null != annotationType) {
			try {
				valiidateType(annotationType.getMethod(key), value);
			} catch (NoSuchMethodException e) {
				// DO NOTHING
			} catch (SecurityException e) {
				throw new RuntimeException(e);
			}
		}
	}

	@Override
	public Object put(String key, Object value) {
		checkMutable();
		valiidateType(key,value);
		return super.put(key, value);
	}
	@Override
	public void putAll(Map<? extends String, ? extends Object> m) {
		checkMutable();
		super.putAll(m);
	}

	@Override
	public void clear() {
		checkMutable();
		super.clear();
	}

	@Override
	public Object remove(Object key) {
		checkMutable();
		return super.remove(key);
	}

	/**
	 * 添加注解实例中所有字段
	 * @param annotation
	 */
	private void addAll(Annotation annotation) {
		checkMutable();
		for(Method method:annotation.annotationType().getMethods()) {
			try {
				// 调用父类方法避免不必要的类型检查 
				super.put(method.getName(), method.invoke(annotation));
			} catch (Exception e) {
				// DO NOTHING
			} 
		}
	}

	private void checkMutable(){
		checkArgument(mutable,"UNSUPPORT OPERATION FOR immutable instance");
	}

	/**
	 * 指定当前对象不可修改
	 */
	public AnnotationAttributes immutable() {
		this.mutable = false;
		return this;
	}

	/**
	 * 实例是否可修改
	 */
	public boolean isMutable() {
		return mutable;
	}

	@Override
	public AnnotationAttributes clone() {
		return new AnnotationAttributes(this,true); 
	}
}
